/*
 * @Autor: dingxiaolin
 * @Date: 2020-12-24 20:46:14
 * @LastEditors: dingxiaolin
 * @LastEditTime: 2020-12-30 00:01:32
 */
var Fly =  require("flyio/dist/npm/wx")
var fly = new Fly(); //创建fly实例
//设置超时
fly.config.timeout = 10000;
console.log('环境',process.env.NODE_ENV);
//配置请求基地址
if(process.env.NODE_ENV === 'development'){
    console.log('开发环境');
    fly.config.baseURL = "http://192.168.10.171:5000" 
	// fly.config.baseURL = "https://lzmz.orvillex.com"
    
}else{
    console.log('生产环境')
	fly.config.baseURL = "https://lzmz.orvillex.com"
}


fly.interceptors.request.use((request, promise) => {
    //  console.log('wx',wx)
    //自定义请求头
    var jwtToken = wx.store.state.user.jwtToken;
    // 或
    // var jwtToken = wx.store.getters['user/jwtToken'];
	console.log('111',jwtToken)
    request.headers["Authorization"] = `Bearer ${jwtToken}`;
    return request;
})
fly.interceptors.response.use((response, promise) => {
    var url = response.request.url
    //只将请求结果的data字段返回
    if (response.data.errorMessage) {
        if (response.data.errorCode == "10001") {
            let that = this;
            wx.showModal({
                title: "提示",
                content: response.data.errorMessage,
                showCancel: false,
                success(res) {
                    if (res.confirm) {
                        wx.clearStorage("cacheUserInfo")
                        wx.clearStorage("cacheToken")
                        wx.reLaunch({
                            url: '/pages/login/index'
                        })
                    }
                }
            });
        }
        if (response.data.errorCode == "110005") { //某个错误需要返回前台
            wx.showModal({
                title: "提示",
                content: response.data.errorMessage,
                showCancel: false,
            });
            return Promise.resolve(response.data.data);
        }
        else {
            wx.showModal({
                title: "提示",
                content: response.data.errorMessage,
                showCancel: false,
            });
            return Promise.reject(response.data.errorMessage)
        }
    } else {
        return Promise.resolve(response.data.data); //标准化
    }
},
    (err, promise) => {
        //发生网络错误后会走到这里
        wx.showToast({
            title: "网络开小差了，请稍后重试",
            icon: "none"
        })
    })

export default fly




// 使用demo:
// request.login({
//     password:"123456",
//     userName:"货准达测试账户",
//     weChatOpenid:""
//  }).then(res=>{
//    console.log(res)
//  })
//get请求可以也可传obj，序列化数据。 //  test?id=123
//  request.test({
//     id: 133
//  })
